package com.sunny.openplatformutils.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import com.sunny.openplatformutils.Alipay;
import com.sunny.openplatformutils.Alipay.BizContent;
import com.sunny.openplatformutils.Alipay.TradeInfo;

/**
 * Hello world!
 *
 */
@Controller
@EnableAutoConfiguration
public class App {

	@GetMapping("/")
	@ResponseBody
	public String home(HttpServletRequest request) {
		return "Hello word!";
	}

	@GetMapping("/qq")
	@ResponseBody
	public String qq(HttpServletRequest request) {
		try {
			Oauth oauth = new Oauth();
			AccessToken accessToken = oauth.getAccessTokenByRequest(request);
			String token = accessToken.getAccessToken();
			System.out.println(token);

			OpenID openIDObj = new OpenID("08ADD6C842291EE9E5B581DC4290ECA8");
			String openID = openIDObj.getUserOpenID();
			System.out.println(openID);
			UserInfo userInfo = new UserInfo("08ADD6C842291EE9E5B581DC4290ECA8", "9983977AF6470C7EE87DC78C32CB5C82");
			UserInfoBean userInfoBean = userInfo.getUserInfo();
			System.out.println("ret:" + userInfoBean.getRet() + ", msg:" + userInfoBean.getMsg());
		} catch (QQConnectException e) {
			e.printStackTrace();
		}
		return "Hello QQ!";
	}

	static String tradeNo = "201706091815210001";// + new Random().nextInt();
	
	@GetMapping("/alipay")
	public void alipay(Double amount, HttpServletResponse response) throws Exception {
		try(PrintWriter pw = response.getWriter()) {
			response.setContentType("text/html;charset=UTF-8");
			BizContent bizContent = new BizContent();
			bizContent.setSubject("iphone");
			bizContent.setBody("iphone 8");
			bizContent.setOut_trade_no(tradeNo);
			bizContent.setTotal_amount(amount);
			bizContent.setProduct_code("FAST_INSTANT_TRADE_PAY");
			String form = Alipay.pagePay(bizContent, "http://test-www.wead.cc/wdsp/finance/recharge?pay=1&cid=1000389", null);
			pw.write(form);
			pw.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@GetMapping("/querystatus")
	@ResponseBody
	public String queryStatus() throws Exception {
		TradeInfo info = Alipay.queryStatus(tradeNo);
		return info.getCode() + ":" + info.getTradeNo() + ":" + info.getStatus();
	}
	
	@GetMapping("/close")
	@ResponseBody
	public String close() throws Exception {
		return "<script language=\"javascript\">window.onload=function(){window.opener=null;window.open('','_self');window.close();}</script>";
	}

	public static void main(String[] args) throws Exception {
		String appId = "2016080500176090", 
				signType = "RSA2",
				serverUrl = "https://openapi.alipaydev.com/gateway.do", 
				publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwdMHuLDzlB1XvoW14JVKJac6XOQWniHAbaFxLvjAf9uH8940X1Q97Vqmj80o7e/j5BknbSwXDv/XOy7cN5IzR4j8VP1mDBhUHuw6FbLV6DkDBJYGGFDE3y6wtXO2TVaQu1Q0LtPYPEC2zXHoccrZUh9DaPgc7OudqvB1czui1dieDjQ2cWOsghgY6o+T2Yz2SaXj5emX7FI170xavbg3zPZ260Op1KKSNscqTxeBwqDoW64dwJudicwh3hjHO0eKTutZiPxO8hHoqF4CLwYY5jIOeHj0+t65OtxBvTIQt41x6TZsR5DVdMc2DxVS2ckNW9w3CgduQGT4/70KK2NZNwIDAQAB",
				privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCuYFvEYb7WelwvTM/TaIDXfGVx1QaBJKnHfi5EIinkqcjE31KPSrvcXDIcpg/VMPPFlkDwv6rHPE+JCTG9T1JaP7Nz+5gSirBxjv7hydTa1Gvjo0u0TUihF83Jsnze3yUy9Ak8YZZPxIRRK9LtYGKB7dF6s46oTyWvJyXDz7Y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0EBe+A5snQq1BJi/UQ4fpdAEkSeBRwHx3PGTg2T0BrwKXJbs/03tns7OEa0JiARU3Qt8BrsAvEdfhzkgDkt+TaCQFBF95A0RX4Rc/kZ9eCgfebO3H6ML29GZMZcfPoX4Vphay517W3ia9phqC6gkV0IhuxI1AoGAZyX7pdiysm/XSuTKGqZeHZPO0En9guZH6h5Pqwur4uLsrWfDvEQJ8iD21VHGcVaafYRiBF3JQzrAhZoXMxokA2ktMveg4EhqSgdFTm11OJlbc9h5xoswA4hARQOHvt7e/iQ4Dq2c+6RDenKZsEnYPEEMborNUErxgyezyWM6sIc=";
		
		
		Alipay.init(serverUrl, appId, privateKey, publicKey, signType);
//		AlipayUtil.pagePay(tradeNo, 0.01, "iphone6s", "iphone6s 64G", null, "http://test.appganhuo.com", null);
//		TradeInfo info = Alipay.queryStatus(tradeNo);
//		System.out.println(info.getStatus());
		SpringApplication.run(App.class, args);
	}
}
